package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.bo.CouponListBo;
import com.cskaoyan.market.bo.ListUserBo;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.service.AdminCouponService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminCouponServiceImpl implements AdminCouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    @Override
    public List<MarketCoupon> list(CouponListBo couponListBo) {
        String page = couponListBo.getPage();
        String limit = couponListBo.getLimit();
        String sort = couponListBo.getSort();
        String order = couponListBo.getOrder();
        String name = couponListBo.getName();
        String type = couponListBo.getType();
        String status = couponListBo.getStatus();
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(type)){
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        if(!StringUtils.isEmpty(status)){
            criteria.andTypeEqualTo(Short.valueOf(status));
        }
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        return marketCoupons;
    }

    @Override
    public void delete(Integer id) {
        marketCouponMapper.deleteByPrimaryKey(id);
    }

    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        marketCouponMapper.updateByPrimaryKey(marketCoupon);
        return marketCoupon;
    }

    @Override
    public List<MarketCouponUser> listuser(ListUserBo listUserBo) {
        String page = listUserBo.getPage();
        String limit = listUserBo.getLimit();
        String sort = listUserBo.getSort();
        String order = listUserBo.getOrder();
        String couponId = listUserBo.getCouponId();
        String status = listUserBo.getStatus();
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        if(!StringUtils.isEmpty(couponId)){
            criteria.andCouponIdEqualTo(Integer.valueOf(couponId));
        }
        if(!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        marketCouponUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        return marketCouponUsers;
    }

    @Override
    public MarketCoupon insert(MarketCoupon marketCoupon) {
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());
        marketCouponMapper.insert(marketCoupon);
        return marketCoupon;
    }

}
